/** @brief Test the major kangmoDB interfaces defined in stgKangmoDB class
 *
 *  testDatabase.cpp
 *  kangmodb
 *
 *  Created by 강모 김 on 11. 4. 11..
 *  Copyright 2011 강모소프트. All rights reserved.
 *
 */
#include "gtest/gtest.h"

#include "kangmodb.h"

/* tests to implement
 [ST] means single thread, [MT] means multi thread
 IUDS ; Insert, Update, Delete, Select

 testCommon.cpp : common routines for test cases. 
 testDatabase.cpp : [ST] create/drop database/table
 testTable.cpp : [ST] IUDS into single table
 testTrans.cpp : [ST] alloc, free, begin, rollback, commit a transaction
 TODO : test_seek.cpp : [ST] seekMin, seekMax, current, prev, next 
 TODO : test_mt_trans.cpp : [MT] alloc, free, begin, rollback, commit transactions
 TODO : test_log.cpp : [ST] write, read log records from a log file
 TODO : test_recovery.cpp : [ST] IUD with active transactions, test recovery
 TODO : [MT] IUDS into single table
 TODO : [MT] IUDS into multi tables
 
*/

/*
// To use a test fixture, derive a class from testing::Test.
class QueueTest : public testing::Test {
protected:  // You should make the members protected s.t. they can be
	// accessed from sub-classes.
	
	// virtual void SetUp() will be called before each test is run.  You
	// should define it if you need to initialize the varaibles.
	// Otherwise, this can be skipped.
	virtual void SetUp() {
		q1_.Enqueue(1);
		q2_.Enqueue(2);
		q2_.Enqueue(3);
	}
	
	// virtual void TearDown() will be called after each test is run.
	// You should define it if there is cleanup work to do.  Otherwise,
	// you don't have to provide it.
	//
	// virtual void TearDown() {
	// }
	
	// A helper function that some test uses.
	static int Double(int n) {
		return 2*n;
	}
	
	// A helper function for testing Queue::Map().
	void MapTester(const Queue<int> * q) {
		// Creates a new queue, where each element is twice as big as the
		// corresponding one in q.
		const Queue<int> * const new_q = q->Map(Double);
		
		// Verifies that the new queue has the same size as q.
		ASSERT_EQ(q->Size(), new_q->Size());
		
		// Verifies the relationship between the elements of the two queues.
		for ( const QueueNode<int> * n1 = q->Head(), * n2 = new_q->Head();
			 n1 != NULL; n1 = n1->next(), n2 = n2->next() ) {
			EXPECT_EQ(2 * n1->element(), n2->element());
		}
		
		delete new_q;
	}
	
	// Declares the variables your tests want to use.
	Queue<int> q0_;
	Queue<int> q1_;
	Queue<int> q2_;
};

// When you have a test fixture, you define a test using TEST_F
// instead of TEST.

// Tests the default c'tor.
TEST_F(QueueTest, DefaultConstructor) {
	// You can access data in the test fixture here.
	EXPECT_EQ(0u, q0_.Size());
}

// Tests Dequeue().
TEST_F(QueueTest, Dequeue) {
	int * n = q0_.Dequeue();
	EXPECT_TRUE(n == NULL);
	
	n = q1_.Dequeue();
	ASSERT_TRUE(n != NULL);
	EXPECT_EQ(1, *n);
	EXPECT_EQ(0u, q1_.Size());
	delete n;
	
	n = q2_.Dequeue();
	ASSERT_TRUE(n != NULL);
	EXPECT_EQ(2, *n);
	EXPECT_EQ(1u, q2_.Size());
	delete n;
}

// Tests the Queue::Map() function.
TEST_F(QueueTest, Map) {
	MapTester(&q0_);
	MapTester(&q1_);
	MapTester(&q2_);
}
*/

/** @brief Test the major kangmoDB interfaces defined in stgKangmoDB class
 */
class DatabaseTest : public testing::Test {
protected:  // You should make the members protected s.t. they can be
	// accessed from sub-classes.
	
	// virtual void SetUp() will be called before each test is run.  You
	// should define it if you need to initialize the varaibles.
	// Otherwise, this can be skipped.
	virtual void SetUp() {
	

		db_ = new stgKangmoDB();
		
		ASSERT_TRUE( db_->initialize("Setup_logfile.dat") == 0 );
		
		// Drop the database if any
		db_->dropDatabase();
				
		// create succeeds
		ASSERT_TRUE( db_->createDatabase() == 0 );
		
		// open succeeds
		ASSERT_TRUE( db_->openDatabase() == 0 );
		
		// alloc succeeds
		ASSERT_TRUE( db_->beginTransaction( & tx_ ) == 0 );
		// tx should NOT be NULL
		ASSERT_TRUE( tx_ != NULL );
	}
	
	// virtual void TearDown() will be called after each test is run.
	// You should define it if there is cleanup work to do.  Otherwise,
	// you don't have to provide it.
	//
	virtual void TearDown() {
		// free succeeds
		ASSERT_TRUE( db_->endTransaction( tx_ ) == 0 );
		
		// close succeeds
		ASSERT_TRUE( db_->closeDatabase() == 0 );
		
		// Drop succeeds because the database is not open
		ASSERT_TRUE( db_->dropDatabase() == 0 );
		
		// Destroy the database object
		ASSERT_TRUE( db_->destroy() == 0 );
	}
protected:
	stgKangmoDB * db_;
	stgTransaction * tx_;
	
};


TEST_F(DatabaseTest, DISABLED_DatabaseBasic) {
	stgKangmoDB db;

	ASSERT_TRUE( db.initialize("logfile.dat") == 0 );
	// Drop the database if any
	db.dropDatabase();

	// Open fails because no database exists
	ASSERT_FALSE( db.openDatabase() == 0 );
	
	// Close fails because no database is open
	ASSERT_FALSE( db.closeDatabase() == 0 );
	
	// Success for the first creation
	ASSERT_TRUE( db.createDatabase() == 0 );
	
	// Failure for the first creation
	ASSERT_TRUE( db.createDatabase() == 0 );
	
	// Open succeeds because the database exists
	ASSERT_TRUE( db.openDatabase() == 0 );
	
	// Drop fails because the database is open
	ASSERT_FALSE( db.dropDatabase() == 0 );
	
	// Close succeeds because the database was open
	ASSERT_TRUE( db.closeDatabase() == 0 );
	
	// Close fails because no database is open
	ASSERT_FALSE( db.closeDatabase() == 0 );
	
	// Drop succeeds because the database is not open
	ASSERT_TRUE( db.dropDatabase() == 0 );
	
	// Open fails because the database was dropped
	ASSERT_FALSE( db.openDatabase() == 0 );

	// Destroy the database object
	ASSERT_TRUE( db.destroy() == 0 );
}


TEST_F(DatabaseTest, DISABLED_TransactionBasic) {
	stgKangmoDB db;

	ASSERT_TRUE( db.initialize("logfile.dat") == 0 );

	// Drop the database if any
	db.dropDatabase();
	
	stgTransaction * tx = NULL;
	
	// alloc fails because no database exists
	ASSERT_FALSE( db.beginTransaction( & tx ) == 0 );
	// tx should be NULL
	ASSERT_TRUE( tx == NULL );
	
	// create succeeds
	ASSERT_TRUE( db.createDatabase() == 0 );

	// alloc fails because no database is open
	ASSERT_FALSE( db.beginTransaction( & tx ) == 0 );
	// tx should be NULL
	ASSERT_TRUE( tx == NULL );
	
	// open succeeds
	ASSERT_TRUE( db.openDatabase() == 0 );
	
	// begin succeeds
	ASSERT_TRUE( db.beginTransaction( & tx ) == 0 );
	// tx should NOT be NULL
	ASSERT_TRUE( tx != NULL );

	// close fails becuase there is an open transaction
	ASSERT_FALSE( db.closeDatabase() == 0 );

	// end succeeds
	ASSERT_TRUE( db.endTransaction( tx ) == 0 );
	
	// close succeeds
	ASSERT_TRUE( db.closeDatabase() == 0 );

	// Drop succeeds because the database is not open
	ASSERT_TRUE( db.dropDatabase() == 0 );

	// Destroy the database object
	ASSERT_TRUE( db.destroy() == 0 );
}

// TODO : test if xxxTable functions fail if database is closed.

TEST_F(DatabaseTest, DISABLED_TableBasic) {
	stgTableId tableId;
	stgTableCursor * cursor;

	// the transaction is already begun
	{
		// drop table fails on meta table( tableId 0 )
		ASSERT_FALSE( db_->dropTable( tx_, 0) == 0 );
		
		// drop table fails because the table does not exist ( tableId 1 )
		ASSERT_FALSE( db_->dropTable( tx_, 1) == 0 );

		// drop table fails because the table does not exist ( tableId -1 )
		ASSERT_FALSE( db_->dropTable( tx_, -1) == 0 );
		
		// drop table fails because the table does not exist ( tableId 65535 )
		ASSERT_FALSE( db_->dropTable( tx_, 65535) == 0 );
		
		// create table succeeds
		ASSERT_TRUE( db_->createTable( tx_, & tableId) == 0 );
	}
	// commit transaction
	ASSERT_TRUE( db_->commitTransaction( tx_ ) == 0 );

	{
		// open table succeeds
		ASSERT_TRUE( db_->openTable( tx_, tableId, & cursor) == 0 );
		
		
		// close table succeeds
		ASSERT_TRUE( db_->closeTable( tx_, cursor) == 0 );
	}
	// commit transaction
	ASSERT_TRUE( db_->commitTransaction( tx_ ) == 0 );


	{
		// drop table succeeds
		ASSERT_TRUE( db_->dropTable( tx_, tableId) == 0 );
	}
	// commit transaction
	ASSERT_TRUE( db_->commitTransaction( tx_ ) == 0 );
}
